package com.ymatou.iapi.im.testcase;

import com.ymatou.iapi.im.parameter.SendMessageToSellerBean;
import com.ymatou.iapi.im.parameter.switchSellerCSSessionBean;
import com.ymatou.iapi.im.service.switchSellerCSSessionCall;
import com.ymatou.iapi.userregister.parameter.UserRegisterInfoRequestDtoBean;
import com.ymatou.iapi.userregister.service.UserRegisterInfoRequestDtoCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.ImCallService;
import com.ymttest.business.service.JUserCallService;
import com.ymttest.database.model.YmtMessage;
import com.ymttest.database.sqlwapper.ImDefaultWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.junit.*;
import org.junit.experimental.categories.Category;

import java.util.Date;
import java.util.Map;

/**
*切换买手会话
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_switchSellerCSSession{
	private static switchSellerCSSessionBean switchsellercssessionBean;
	private static switchSellerCSSessionCall switchsellercssessionCall;
	private static int sellerId = Integer.parseInt(EnvSetup.getData("autotest_seller1"));
	private static String sellername = EnvSetup.getData("autotest_sellerName1");
	private static int zsellerId = Integer.parseInt(EnvSetup.getData("autotest_seller_z"));
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("切换买手会话");
	}

	@Before
	public void caseUp() {
		switchsellercssessionBean=new switchSellerCSSessionBean();
		switchsellercssessionCall=new switchSellerCSSessionCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(switchsellercssessionCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
		public void Tc_switchSellerCSSession_001() {
		Logger.start(true,"切换买手会话");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "nihao world";
			SendMessageToSellerBean sendMessageToSellerBean = ImCallService.initSendMessageToSellerBean(sellerId,userId,message);
			String messageId = ImCallService.SendMessageToSeller(sendMessageToSellerBean).get("messageId").getAsString();
			ImDefaultWapper im = new ImDefaultWapper();
			YmtMessage ymtMessage = im.selectImInfo(messageId).get(0);
			Map map = im.selectTotalCount( userId,sellerId).get(0);
			Date time = convertLongTimeToDateTime(Long.parseLong(map.get("LLastMessageTime").toString())).toDate();
			Logger.debug(map);
			String newSessionNumber = TestDataManager.getRandomNum(6);
			Integer newCsid = Integer.parseInt(TestDataManager.getRandomNum(6));
			String newCsName = "niuniu";

			switchsellercssessionBean.setFromUserId(userId);
			switchsellercssessionBean.setToUserId(sellerId);
			switchsellercssessionBean.setOldSessionNumber(ymtMessage.getSessionNumber());
			switchsellercssessionBean.setNewSessionNumber(newSessionNumber);
			switchsellercssessionBean.setNewCsId(newCsid);
			switchsellercssessionBean.setNewCsName(newCsName);
			switchsellercssessionBean.setUnReadCount(1);
			switchsellercssessionBean.setLastMessageTime(time);
			switchsellercssessionCall.setData(switchsellercssessionBean);
			switchsellercssessionCall.callService();

			Logger.verifyEquals("Success",switchsellercssessionCall.getString("Status"),"验证Status");
			Map map1 = im.selectTotalCount(userId,newCsid).get(0);
			Logger.verifyEquals(userId,Integer.parseInt(map1.get("OtherUserId").toString()),"验证是否是同一个买家");
			Logger.verifyEquals(newCsid,Integer.parseInt(map1.get("UserId").toString()),"验证是否是新客服");
			//验证未读数
			Logger.verifyEquals(1,Integer.parseInt(map1.get("UnReadCount").toString()),"验证未读消息数");
			//验证消息总数
			Logger.verifyEquals(1,Integer.parseInt(map1.get("TotalCount").toString()),"验证消息总数");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_switchSellerCSSession_002() {
		Logger.start(true,"切换买手会话,免打扰");
		try {
			UserRegisterInfoRequestDtoBean regbean = JUserCallService.initUserRegisterInfoRequestDtoBean();
			UserRegisterInfoRequestDtoCall urrc = JUserCallService.UserRegisterInfoRequestDtoCall(regbean);
			Integer userId = urrc.getUserid();
			String message = "nihao world";
			SendMessageToSellerBean sendMessageToSellerBean1 = ImCallService. initSendMessageToSellerBean(sellerId,userId,message);
			SendMessageToSellerBean sendMessageToSellerBean2 = ImCallService. initSendMessageToSellerBean(sellerId,userId,message);
			String messageId1 = ImCallService.SendMessageToSeller(sendMessageToSellerBean1).get("messageId").getAsString();
			String messageId2 = ImCallService.SendMessageToSeller(sendMessageToSellerBean2).get("messageId").getAsString();
			ImDefaultWapper im = new ImDefaultWapper();
			YmtMessage ymtMessage = im.selectImInfo(messageId1).get(0);
			Map map = im.selectTotalCount( userId,sellerId).get(0);
			Date time = convertLongTimeToDateTime(Long.parseLong(map.get("LLastMessageTime").toString())).toDate();
			Logger.debug(map);

//			String newSessionNumber = TestDataManager.getRandomNum(6);
//			Integer newCsid = Integer.parseInt(TestDataManager.getRandomNum(6));
//			String newCsName = "niuniu";
//
//			switchsellercssessionBean.setFromUserId(userId);
//			switchsellercssessionBean.setToUserId(sellerId);
//			switchsellercssessionBean.setOldSessionNumber(ymtMessage.getSessionNumber());
//			switchsellercssessionBean.setNewSessionNumber(newSessionNumber);
//			switchsellercssessionBean.setNewCsId(newCsid);
//			switchsellercssessionBean.setNewCsName(newCsName);
//			switchsellercssessionBean.setUnReadCount(1);
//			switchsellercssessionBean.setLastMessageTime(time);
//			switchsellercssessionCall.setData(switchsellercssessionBean);
//			switchsellercssessionCall.callService();
//
//			Logger.verifyEquals("Success",switchsellercssessionCall.getString("Status"),"验证Status");
//			Map map1 = im.selectTotalCount(userId,newCsid).get(0);
//			Logger.verifyEquals(userId,Integer.parseInt(map1.get("OtherUserId").toString()),"验证是否是同一个买家");
//			Logger.verifyEquals(newCsid,Integer.parseInt(map1.get("UserId").toString()),"验证是否是新客服");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	public DateTime convertLongTimeToDateTime(long longdata) {
		DateTimeFormatter format = DateTimeFormat.forPattern("yyyyMMddHHmmssSSS");
		return DateTime.parse(longdata + "", format);
	}
}